//
//  XInformationReviewViewController.m
//  Bees
//
//  Created by Xiaohan on 2018/12/21.
//  Copyright © 2018 xiaohan. All rights reserved.
//

#import "XInformationReviewViewController.h"
#import "XHomeSegementView.h"
#import "XinfomationReviewSubViewController.h"
#import "XHFullPopContentScrollView.h"
@interface XInformationReviewViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong) XHFullPopContentScrollView *backScrollView;
@property (nonatomic,weak) XHomeSegementView *segementView;
@end

@implementation XInformationReviewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI{

    XHomeSegementView *segementView = [[XHomeSegementView alloc]initWithFrame:CGRectMake(0, 0, AdaptedWidth(180), KHomeSegementHeight)];
    _segementView = segementView;
    segementView.fixedWidth = AdaptedWidth(60);
    segementView.titles = @[XLocalized(@"资讯"),XLocalized(@"评测"),XLocalized(@"公告")];

    kWeakSelf(self);
    segementView.resultBlock = ^(NSInteger index) {
        [weakself showVC:index];
    };
    self.navigationItem.titleView = segementView;
    self.backScrollView = [[XHFullPopContentScrollView alloc]initWithFrame:self.view.bounds];
    self.backScrollView.contentSize = CGSizeMake(3*kScreenWidth, 0);
    self.backScrollView.pagingEnabled = YES;
    self.backScrollView.delegate = self;
    self.backScrollView.bounces = NO;
    [self.view addSubview:self.backScrollView];
    
    XinfomationReviewSubViewController*infomationVC = [XinfomationReviewSubViewController new];
    infomationVC.type = InfomationReviewTypeInfomation;
   
    XinfomationReviewSubViewController*reviewVC = [XinfomationReviewSubViewController new];
    reviewVC.type = InfomationReviewTypeReview;
    
    XinfomationReviewSubViewController*nVC = [XinfomationReviewSubViewController new];
    nVC.type = InfomationReviewTypeNotice;
    
    [self addChildViewController:infomationVC];
    [self addChildViewController:reviewVC];
    [self addChildViewController:nVC];
    [self showVC:0];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat offset = scrollView.contentOffset.x;
    NSInteger currentIndex = offset/kScreenWidth;
    self.segementView.selIndex = currentIndex;
    [self showVC:currentIndex];
}

- (void)showVC:(NSInteger)index{
    UIViewController *vc = self.childViewControllers[index];
    //判断控制器的view有没有加载过,如果已经加载过,就不需要加载
    if (!vc.isViewLoaded) {
        vc.view.frame = CGRectMake(kScreenWidth*index, 0, kScreenWidth,self.backScrollView.height);
        [self.backScrollView addSubview:vc.view];
    }
    [self.backScrollView setContentOffset:CGPointMake(kScreenWidth*index, 0) animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
